package chapter1.zyq1_str_obj;

import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.List;

public class Client4{
    public static void main(String[] args) {
        Client4 client = new Client4();
        client.communicate();
    }

    public void communicate() {
        InetSocketAddress isAddr = new InetSocketAddress("localhost",8001);
        Socket socket = new Socket();
        try {
            //设置客户端的连接超时为30000毫秒
            socket.connect(isAddr , 300000 );
            System.out.println("客户端连接成功，开始会话...");
            //设置客户端的读超时为20000毫秒
            socket.setSoTimeout(20000);
            session(socket);
            socket.close();
            System.out.println("会话结束，客户端关闭连接。");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    protected void session(Socket socket) throws Exception {
        Student4 stu = (Student4)sendReceiveUtil_Obj4.recvObject(socket);
        System.out.println("接收对象: " + stu);
        sendReceiveUtil_Obj4.sendObject(socket, stu);
        System.out.println("发送对象: " + stu);
        sendReceiveUtil_Obj4.sendObject(socket, stu);
        System.out.println("发送对象: " + stu);
        List<Student4> stuList = (List<Student4>)sendReceiveUtil_Obj4.recvObject(socket);
        System.out.println("接收列表: " + stuList);
    }


}